<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@include file="../common/header.jsp"%>
<%--广告ad列表--%>
<div class="easyui-layout" data-options="fit:true">
    <!-- Begin of toolbar -->
    <div id="wu-toolbar">
        <%--左边菜单栏--%>
        <div class="wu-toolbar-button">
            <%@include file="../common/menus.jsp"%>
        </div>

        <%--搜索工具栏--%>
        <div class="wu-toolbar-search">
            <label>名称:</label><input id="search-name" class="wu-text" style="width:100px">
            <label>所属类型:</label>
            <select id="search-type" class="easyui-combobox" panelHeight="auto" style="width:120px">
            	<option value="-1">全部</option>
            	<option value="1">轮播</option>
            	<option value="2">侧边</option>
                <option value="3">链接</option>
            </select>
            <a href="#" id="search-btn" class="easyui-linkbutton" iconCls="icon-search">搜索</a>
        </div>
    </div>
    <!-- End of toolbar -->

    <%--数据域  data table--%>
    <table id="data-datagrid" class="easyui-datagrid" toolbar="#wu-toolbar"></table>
</div>

<!-- Begin of easyui-dialog -->
<%@include file="../common/footer.jsp"%>
<!-- End of easyui-dialog -->
<script type="text/javascript">

    /**
     * 打开添加窗口 add.jsp
     */
    function openAdd(){
        //$('#add-form').form('clear');
        window.location.href = 'add';
    }

	/**
	* 删除记录
	*/
	function remove(){
		$.messager.confirm('信息提示','确定要删除该记录？', function(result){
			if(result){
				var item = $('#data-datagrid').datagrid('getSelections');
				if(item == null || item.length == 0){
					$.messager.alert('信息提示','请选择要删除的数据！','info');
					return;
				}
				var ids = '';
				for(var i=0;i<item.length;i++){
					ids += item[i].id + ',';
				}
				$.ajax({
					url:'delete',
					dataType:'json',
					type:'post',
					data:{ids:ids},
					success:function(data){
						if(data.type == 'success'){
							$.messager.alert('信息提示','删除成功！','info');
							$('#data-datagrid').datagrid('reload');
						}else{
							$.messager.alert('信息提示',data.msg,'warning');
						}
					}
				});
			}	
		});
	}

    /**
     * 删除记录
     */
    function check(){
        $.messager.confirm('信息提示','确定要check该记录？', function(result){
            if(result){
                var item = $('#data-datagrid').datagrid('getSelections');
                if(item == null || item.length == 0){
                    $.messager.alert('信息提示','请选择要check的数据！','info');
                    return;
                }
                var ids = '';
                for(var i=0;i<item.length;i++){
                    ids += item[i].id + ',';
                }
                $.ajax({
                    url:'check',
                    dataType:'json',
                    type:'post',
                    data:{ids:ids},
                    success:function(data){
                        if(data.type == 'success'){
                            $.messager.alert('信息提示','check成功！','info');
                            $('#data-datagrid').datagrid('reload');
                        }else{
                            $.messager.alert('信息提示',data.msg,'warning');
                        }
                    }
                });
            }
        });
    }

    /**
     * 打开修改窗口
     */
    function openEdit(){
        //$('#edit-form').form('clear');
        var item = $('#data-datagrid').datagrid('getSelected');
        if(item == null || item.length == 0){
            $.messager.alert('信息提示','请选择要修改的数据！','info');
            return;
        }
        window.location.href = 'edit?id=' + item.id;
    }
	
	//搜索按钮监听
	$("#search-btn").click(function(){
		var type = $("#search-type").combobox('getValue')
		var option = {name:$("#search-name").val()};
		if(type != -1){
			option.type = type;
		}
		$('#data-datagrid').datagrid('reload',option);
	});

    function add0(m){return m<10?'0'+m:m }
    function format(shijianchuo){
        //shijianchuo是整数，否则要parseInt转换
        var time = new Date(shijianchuo);
        var y = time.getFullYear();
        var m = time.getMonth()+1;
        var d = time.getDate();
        var h = time.getHours();
        var mm = time.getMinutes();
        var s = time.getSeconds();
        return y+'-'+add0(m)+'-'+add0(d)+' '+add0(h)+':'+add0(mm)+':'+add0(s);
    }


	/** 
	* 载入数据
	*/
	$('#data-datagrid').datagrid({
		url:'list',
		rownumbers:true,
		singleSelect:false,
		pageSize:20,           
		pagination:true,
		multiSort:true,
		fitColumns:true,
		idField:'id',
	    treeField:'name',
		fit:true,
		columns:[[
			{ field:'chk',checkbox:true},
            { field:'name',title:'名称',width:100,sortable:true},
            { field:'content',title:'内容',width:200},
			{ field:'path',title:'图像',width:100,align:'center',formatter:function(value,row,index){
				var img = '<img src="'+value+'" width="50px" />';
				return img;
			}},
            { field:'link',title:'链接',width:150,formatter:function(value,row,index){
                    var link = '<a href="'+value+'" target="_blank">'+value+'</a>';
                    return link;
            }},
			{ field:'type',title:'类型',width:20,formatter:function(value,row,index){
				switch(value){
					case 0:{
						return '未知';
					}
					case 1:{
						return '轮播';
					}
					case 2:{
						return '侧边';
					}
                    case 3:{
                        return '链接';
                    }
				}
				return value;
			}},
            { field:'createtime',title:'创建时间',sortable:true,width:100,formatter:function(value,row,index){
                    return format(value);
                }
            },
            { field:'status',title:'是否显示',width:20,formatter:function(value,row,index){
                    if (value ==1 ) {
                        return '<span style=color:green; >显示</span>';
                    } else if (value ==0 ) {
                        return '<span style=color:red; >失效</span>';
                    }
                }
            }
		]],
		onLoadSuccess:function(data){  
			$('.authority-edit').linkbutton({text:'编辑权限',plain:true,iconCls:'icon-edit'});  
        }
	});
</script>